from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_script import Manager
import os

class Application(Flask):
    def __init__(self, import_name, template_folder=None, root_path=None):
        super(Application, self).__init__(import_name, template_folder=template_folder, root_path=root_path, static_folder=None)
        self.config.from_pyfile('config/base_setting.py')  # 加载配置文件

        # self.config.from_pyfile('config/local_setting.py')  # 加载配置文件
        # self.config.from_pyfile('config/production_setting.py')  # 加载配置文件
        if 'ops_config' in os.environ:  # 实现按不同环境加载配置文件  #
            # 终端输入export ops_config=local 改变系统环境，才能有效果（Linux）
            # self.config.from_pyfile('config/%s_setting.py'%os.environ['ops_config'])
            self.config.from_pyfile(r'config/local_setting.py')
        db.init_app(self)


db = SQLAlchemy()
app = Application(__name__, template_folder=os.getcwd()+"/web/templates/", root_path=os.getcwd())
manager = Manager(app)


"""
函数模板：
将方法注入静态模板里面
"""
from common.libs.UrlManager import UrlManager
app.add_template_global(UrlManager.buildStaticUrl, 'buildStaticUrl')
app.add_template_global(UrlManager.buildUrl, 'buildUrl')
# run :set ops_config=local && python manage.py runserver
